package com.iotek.controller;

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.iotek.entity.Image;
import com.iotek.service.ImageService;
import com.iotek.util.QNYUtil;

@Controller
public class ImageController {
	@Autowired
	private ImageService imageServiceImpl;
	
	//图片上传
	@RequestMapping("/fileup.action")
	public String fileUp(@RequestParam MultipartFile file,HttpServletRequest  request,Image image) throws IOException {				
		
		InputStream inputStream = file.getInputStream();
		String filename = file.getOriginalFilename();
		//System.out.println(QNYUtil.upToQNY(inputStream, filename));	
		String newFileName = QNYUtil.upToQNY(inputStream, filename);		 
		image.setImgAddress(newFileName);
	    imageServiceImpl.addImage(image);
		
	    return "success";
	}
	
	//图片查看
	@RequestMapping("/findimgAddress.action")
	public String findimgAddress(Image image, String imgAddress,HttpSession session, HttpServletRequest request) {
		String address = request.getParameter("goodsImg");
		String imgaddress = imageServiceImpl.findimgAddress(address);
		session.setAttribute("imgaddress", imgaddress);
		
		return "success";
	}
	
}
